//
// Created by Administrator on 2023/11/23.
//
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */

#include "bits/stdc++.h"

using namespace std;

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;

    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
    int dfs(TreeNode *node, int depth) {
        if (!node)
            return 0;
        else if (!node->left && !node->right)
            return depth * node->val;

        return dfs(node->right, depth + 1) + dfs(node->left, depth + 1);
    }

    int pathSum(TreeNode *root) {

        return dfs(root, 0);

    }
};